package shopping.management;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.commons.BaseAction;
import shopping.model.Order;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: mazhao
 * Date: 2010/11/01
 * Time: 23:07:04
 * To change this template use File | Settings | File Templates.
 */
public class OrderAction extends BaseAction {

    // ===========================================
    // for page rendering
    // ===========================================

    public Map<Integer, String> getOrderStatus() {
        return Order.status;
    }


    // ===========================================
    // for data communication with pages
    // ===========================================

    // ========= for displaying only =========
    private List<Order> orders = new ArrayList<Order>();

    public List<Order> getOrders() {
        return orders;
    }


    // ========= for displaying and receiving =========

    private int status;
    private String from;
    private String to;

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    // ===========================================
    // action methods
    // ===========================================

    @Action(value = "/management/orderlist",
            results = {
                    @Result(location = "/WEB-INF/content/management/order.jsp", name = "success")
            }
    )
    public String list() {
        log.debug("enter /management/orderlist action");

        log.debug("status:" + status);
        log.debug("  from:" + from);
        log.debug("    to:" + to);

        Map param = new HashMap();
        if(status != 0) {
            param.put("status", status);
        } 

        if(from != null && from.length() != 0) {
            param.put("from", from);
        }
        
        if(from != null && from.length() != 0 && (to == null || to.length() == 0)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd") ;
            to = sdf.format(new Date());
        }

        if(to != null && to.length() != 0) {
            param.put("to", to);
        }

        orders = Order.dynaQuery(param);

        if (log.isDebugEnabled()) {
            for (Order o : orders) {
                log.debug(o);
            }
        }

        return SUCCESS;
    }
}
